package com.xinxing.city.recovery.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class BaseActivity<S extends ViewDataBinding,T extends Object> extends AppCompatActivity {
    public Class<T> clazz;
    protected T t ;
    protected S binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,getLayoutId());
        t = getT();
        init();
        binding.setVariable(getVariableId(),t);
    }

    protected void init(){
        String str = (String) SharedPreferencesUtils.getParam(this,getShare(),"");
        if(!str.equals("")){
            Type dev = new TypeToken<T>() {}.getType();
            Gson gson = new Gson();;
            t = gson.fromJson(str,dev);
        }
    }
    public void saveSub(View view) {
        Gson gson = new Gson();
        SharedPreferencesUtils.setParam(this, getShare(), gson.toJson(t));
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
    }

    /**
     * 设置布局
     *
     * @return
     */
    public abstract int getLayoutId();
    public abstract String  getShare();
    public abstract int getVariableId();
    public abstract T getT();
}
